package com.txcb.lib.base.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * @author : Lambert
 * date   : 2020/10/21 5:09 PM
 */
public class UIUtil {

    public static Application mContext;

    public static void init(Application context){
        mContext = context;
    }
    /**
     * dp转px
     * @param dpValue dp
     * @return px
     */
    public static int dipToPx(  float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @return
     */
    public static int sp2px(  float spValue) {
        final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int getWidowsWidth(Context context) {
        WindowManager manager = ((Activity) context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;

        return width;

    }

    public static int getWidowsHeight(Context context) {
        WindowManager manager = ((Activity) context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int height = outMetrics.heightPixels;

        return height;

    }

    public static String getString(int stringId,String appendStr){
        return mContext.getString(stringId,appendStr);
    }

    public static ColorStateList getSelectColor(int selectColorId){
        ColorStateList colorStateList = mContext.getResources().getColorStateList( selectColorId);
        return colorStateList;
    }

    /*
     * 获取屏幕状态栏高度
     * */
    public static int getStatusBarHeight() {
        Resources resources = mContext.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

}
